package kr.hkit.androidcomplete03.ch20;


import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListFragmentTest extends Activity {
	static String[] WORD;
	static String[] DESC;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		WORD=getResources().getStringArray(R.array.word);
		DESC=getResources().getStringArray(R.array.desc);
		
		FragmentManager fm=getFragmentManager();
		if(fm.findFragmentById(android.R.id.content)==null){
			WordListFragment wf=new WordListFragment();
			fm.beginTransaction().add(android.R.id.content, wf).commit();
		}
	}
	public static class WordListFragment extends ListFragment{
		
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			ArrayAdapter<String> adapter=new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, WORD);
			setListAdapter(adapter);
		}
		
		public void onListItemClick(ListView l, View v, int position, long id) {
			String text=WORD[position] + " : " +DESC[position];
			Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
		}
	}
}
